package com.augurit.gzcgwsp.state.util;

import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class RequestUtil {
	//获取request对象中所有参数，并设置到map中
	public static Map getMapByRequest(HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		Enumeration enu = request.getParameterNames();
		while(enu.hasMoreElements()) {   
            String paraName = (String)enu.nextElement();   
            String paraValue = request.getParameter(paraName).trim(); 
            if(paraValue!=null && !"".equals(paraValue)){
            	map.put(paraName, paraValue);
            }           
		}
				
		return map;
	}
	
	/**
	 * 加上编码
	 * @param request
	 * @return
	 */
	public static Map getMapByRequestAndEncoding(HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		Enumeration enu = request.getParameterNames();
		while(enu.hasMoreElements()) {   
            String paraName = (String)enu.nextElement();   
            String paraValue;
			try {
				paraValue = new String(request.getParameter(paraName).getBytes("iso-8859-1"), "utf-8").trim();
	            if(paraValue!=null && !"".equals(paraValue)){
	            	map.put(paraName, paraValue);
	            }
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}            
		}				
		return map;
	}
}
